import pyodbc

# 定义数据库连接参数
server = '192.168.133.69'
database = 'Yexiong'
username = 'sa'
password = 'Rpa2023'
# 创建连接字符串
conn_str = f'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password};Encrypt=no'


# 连接数据库
def connect_db():
    conn = pyodbc.connect(conn_str)
    cursor = conn.cursor()
    return conn, cursor


def insert_taskid(conn, cursor, taskid_list):
    insert_query = f"INSERT INTO [dbo].[财务采购单审批] ([taskid]) VALUES (?)"
    for taskid in taskid_list:
        try:
            cursor.execute(insert_query, taskid)
        except pyodbc.IntegrityError:
            continue
    # 提交事务
    conn.commit()


def select_taskid(cursor, taskid):
    select_query = f"SELECT [是否处理] FROM [dbo].[财务采购单审批] where taskid={taskid}"
    cursor.execute(select_query)
    result = cursor.fetchall()
    return result[0][0]


def update_taskid(conn, cursor, taskid):
    update_query = f"UPDATE [dbo].[财务采购单审批] SET [是否处理]=1 WHERE taskid={taskid}"
    cursor.execute(update_query)
    # 提交事务
    conn.commit()
